package com.situ.springbootscientific.controller;

import com.situ.springbootscientific.pojo.Project;
import com.situ.springbootscientific.pojo.Researchers;
import com.situ.springbootscientific.pojo.query.ProjectQuery;
import com.situ.springbootscientific.pojo.vo.ProjectIdVo;
import com.situ.springbootscientific.pojo.vo.ProjectVo;
import com.situ.springbootscientific.service.IProjectService;
import com.situ.springbootscientific.utils.JSONUtil;
import com.situ.springbootscientific.utils.PageRusults;
import com.situ.springbootscientific.utils.Result;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/project")
public class ProjectController {
    @Autowired
    private IProjectService projectService;
    @RequestMapping("/list")
    @ResponseBody
    public PageRusults list(ProjectQuery projectQuery) {
        PageRusults pageRusults = projectService.list(projectQuery);
        return pageRusults;
    }
    @RequestMapping("/update")
    public Result update(Project project) {
        projectService.update(project);
        return Result.ok("更新成功");
    }
    @RequestMapping("/selectById")
    public Result selectById(Integer id) {
        System.out.println("ProjectController.selectById");
        ProjectVo projectVo = projectService.selectById(id);
        return Result.ok(projectVo);
    }
    @RequestMapping("/selectByRes")
    public void selectByRes(HttpServletRequest req, HttpServletResponse resp, HttpSession session) {
        System.out.println("ProjectController.selectByRes");
        Researchers researchers = (Researchers) session.getAttribute("researchers");
        Integer id = researchers.getId();
       /* HttpSession session = req.getSession();
        Researchers researchers = (Researchers) session.getAttribute("researchers");
        Integer id = researchers.getId();*/

        //我得在用个Vo把数据封装一下
        List<ProjectVo> projectIdVo = projectService.selectByRes(id);//返回的肯定不是project

        //map.put("list",list);

        JSONUtil.toJSON(resp,Result.ok(projectIdVo));
    }
    @RequestMapping("/add")
    public Result add(ProjectVo projectVo) {
        System.out.println("ProjectController.add");
        projectService.add(projectVo);
        return Result.ok("添加成功");
    }
}
